package com.example.mall.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.mall.constants.BaseConstants;
import com.example.mall.domain.InventoryAlerts;
import com.example.mall.dto.InventoryAlertsDto;
import com.example.mall.mapper.InventoryAlertsMapper;
import com.example.mall.query.InventoryAlertsQuery;
import com.example.mall.query.PageDTO;
import com.example.mall.service.IInventoryAlertsService;
import com.example.mall.util.AssertUtil;
import com.example.mall.util.CodeGenerateUtils;
import com.example.mall.util.CopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 库存预警表 服务实现类
 * </p>
 *
 * @author whale
 * @since 2025-05-09
 */
@Service
public class InventoryAlertsServiceImpl extends ServiceImpl<InventoryAlertsMapper, InventoryAlerts> implements IInventoryAlertsService {

    @Autowired
    private InventoryAlertsMapper inventoryAlertsMapper;

    @Override
    public PageDTO<InventoryAlertsDto> queryPage(InventoryAlertsQuery inventoryAlertsQuery) {
        if (inventoryAlertsQuery == null) {
            inventoryAlertsQuery = new InventoryAlertsQuery();
        }
        Page<InventoryAlerts> page = inventoryAlertsQuery.toMpPageByDefaultCreateTimeAsc();
        Page<InventoryAlerts> cartPage = lambdaQuery()
                .like(inventoryAlertsQuery.getAlertId()!= null, InventoryAlerts::getAlertId, inventoryAlertsQuery.getAlertId())
                .like(inventoryAlertsQuery.getProductId()!= null, InventoryAlerts::getProductId, inventoryAlertsQuery.getProductId())
                .like(inventoryAlertsQuery.getSpecId()!= null, InventoryAlerts::getSpecId, inventoryAlertsQuery.getSpecId())
                .like(inventoryAlertsQuery.getThreshold()!= null, InventoryAlerts::getThreshold, inventoryAlertsQuery.getThreshold())
                .like(inventoryAlertsQuery.getIsActive()!= null, InventoryAlerts::getIsActive, inventoryAlertsQuery.getIsActive())
                .orderByDesc(InventoryAlerts::getUpdateDate)
                .page(page);
        return PageDTO.of(cartPage, InventoryAlertsDto.class);
    }

    @Override
    public InventoryAlerts queryInventoryAlertsById(Long alertId) {
        AssertUtil.isNotNull(alertId, "库存预警ID不能为空");
        return inventoryAlertsMapper.selectById(alertId);
    }

    @Override
    public List<InventoryAlerts> queryInventoryAlertsList() {
        return this.list();
    }

    @Override
    public void insert(InventoryAlertsDto inventoryAlertsDto) {
        InventoryAlerts inventoryAlerts = new InventoryAlerts();
        CopyUtils.copyProperties(inventoryAlertsDto, inventoryAlerts);
        inventoryAlerts.setCreateDate(new Date());
        inventoryAlerts.setUpdateDate(new Date());
        inventoryAlerts.setAlertId(CodeGenerateUtils.generateUniqueId());
        inventoryAlerts.setIsActive(BaseConstants.EFFECTIVE_STATUS);
        inventoryAlertsMapper.insert(inventoryAlerts);
    }

    @Override
    public void update(InventoryAlertsDto inventoryAlertsDto) {
        AssertUtil.isNotNull(inventoryAlertsDto.getAlertId(), "库存预警ID不能为空");
        InventoryAlerts inventoryAlerts = inventoryAlertsMapper.selectById(inventoryAlertsDto.getAlertId());
        AssertUtil.isNotNull(inventoryAlerts, "库存预警记录不存在");
        CopyUtils.copyProperties(inventoryAlertsDto, inventoryAlerts);
        inventoryAlerts.setUpdateDate(new Date());
        inventoryAlertsMapper.updateById(inventoryAlerts);
    }

    @Override
    public void delete(Long alertId) {
        AssertUtil.isNotNull(alertId, "库存预警ID不能为空");
        InventoryAlerts inventoryAlerts = inventoryAlertsMapper.selectById(alertId);
        AssertUtil.isNotNull(inventoryAlerts, "库存预警记录不存在");
        inventoryAlerts.setIsActive(BaseConstants.LOSE_STATUS);
        inventoryAlertsMapper.updateById(inventoryAlerts);
    }

    @Override
    public void deleteBatch(List<Long> alertIds) {
        AssertUtil.isNotNull(alertIds, "库存预警ID列表不能为空");
        List<InventoryAlerts> inventoryAlerts = inventoryAlertsMapper.selectBatchIds(alertIds);
        AssertUtil.isNotNull(inventoryAlerts, "库存预警记录不存在");
        for (InventoryAlerts inventoryAlert : inventoryAlerts) {
            inventoryAlert.setIsActive(BaseConstants.LOSE_STATUS);
            inventoryAlertsMapper.updateById(inventoryAlert);
        }
    }
}
